/**
 * Created by dukes on 2016/3/4.
 */
class CreateGuildPanel extends eui.Component{
    public constructor(){
        super();
        this.skinName = "pet.CreateGuildSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAdded, this );
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE,this.onRemoved, this );
    }

    private onAdded(evt:egret.Event){
        this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.img_ok.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onOk, this);
        this.img_left.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onLeft, this);
        this.img_right.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRight, this);
    }

    private onRemoved(evt:egret.Event){
        this.img_close.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.img_ok.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onOk, this);
        this.img_left.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onLeft, this);
        this.img_right.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onRight, this);
    }
    private curIndex = 1;
    public onClose(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        this.hide();
    }

    public onOk(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        if( this.name_input.text.length == 0){
            ShowHint("家族名未填写");
            return;
        }
        if( Model.me.diamond < 100){
            BuyDiamondPanel.getInstance().show();
            return;
        }
        window["__createGuild"](this.name_input.text,this.curIndex);
    }

    public onLeft(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        this.curIndex--;
        this.updatePageDisplay();
    }

    public onRight(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        this.curIndex++;
        this.updatePageDisplay();
    }

    private updatePageDisplay(){
        if( this.curIndex<1){
            this.curIndex = 1;
        }else if( this.curIndex > 6){
            this.curIndex = 6;
        }
        this.img_medal.source =  GameConst.Res_Root+"resource/assets/medals/"+this.curIndex+".png";
        this.img_left.visible = this.curIndex>1;
        this.img_right.visible = this.curIndex<6;
    }

    public name_input:eui.TextInput;
    public img_close:eui.Image;
    public img_ok:eui.Image;
    public img_medal:eui.Image;
    public img_left:eui.Image;
    public img_right:eui.Image;
    public lab_price:eui.Image;
    public partAdded(name:string, instance:any) {
        super.partAdded(name, instance);
        if (name == "img_close") {
            this.img_close = instance;
        } else if (name == "name_input") {
            this.name_input = instance;
        }else if (name == "img_ok") {
            this.img_ok = instance;
        }else if (name == "img_medal") {
            this.img_medal = instance;
        }else if (name == "img_left") {
            this.img_left = instance;
            this.img_left.visible = false;
        }else if (name == "img_right") {
            this.img_right = instance;
        }else if (name == "lab_price") {
            this.lab_price = instance;
        }
    }
    public show() {
        DisplayController.instance().addToAlert(GameConst.GrayMask);
        DisplayController.instance().addToAlert(this);
    }
    public hide(){
        DisplayController.instance().removeFromAlert(GameConst.GrayMask);
        DisplayController.instance().removeFromAlert(this);
    }

    public childrenCreated(){
        super.childrenCreated();
        this.updatePageDisplay();
    }

    public measure(){
        super.measure();
        this.x = GameConst.StageW - this.measuredWidth >> 1;
        this.y = GameConst.StageH - this.measuredHeight >> 1;
    }

    private static _instance:CreateGuildPanel;
    public static getInstance():CreateGuildPanel{
        if( CreateGuildPanel._instance == null)
            CreateGuildPanel._instance = new CreateGuildPanel();
        return CreateGuildPanel._instance;
    }
}